package net.wsnetwork.jawa.frontend;

import java.net.InetAddress;
import java.util.List;
import java.util.Random;

import net.wsnetwork.jawa.master.BackendInfo;

public class RoundRobinCacheRouter implements CacheRouter {
    private final Random random = new Random();
    private final MasterClient client;

    public RoundRobinCacheRouter(MasterClient client) {
        this.client = client;
    }

    @Override public InetAddress getBackendForRequest(String host, String uri) {
        List<BackendInfo> backends = client.getBackends();
        int index = Math.abs(random.nextInt()) % backends.size();
        return backends.get(index).getAddress();
    }
}
